﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using AliNetpad.Data;

namespace dotnethelloworld
{
    public partial class GetUser : System.Web.UI.Page
    {

        public string apiname = "alisoft.udb.getDomainLoginId";//接口调用名称
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string appInstanceId = Session["app_instance_id"].ToString();
            string appId = Session["app_id"].ToString(); //软件注册时获得
            string userId = TextBox1.Text.Trim();
            System.DateTime timestamp = System.DateTime.Now;
            SortedList mySL = new SortedList();
            mySL.Add("appId", Session["app_id"]);
            //mySL.Add("", "401ae2d0200611de9af2f8a7cfb13186"); 
            //mySL.Add("appInstanceId", appInstanceId);
            //mySL.Add("token", token);
            mySL.Add("sip_appkey", appId);
            mySL.Add("sip_timestamp", timestamp);
            mySL.Add("sip_apiname", apiname);
            mySL.Add("domainid", "3");
            mySL.Add("userid", userId);
            string sipsign = Signature.DoSignature(mySL);


            //系统级参数拼装，包括sip_appkey、sip_apiname、sip_timestamp和sip_sign，sip_apiname是接口名这里举validateUser为例
            string SIPdate = "sip_appkey=" + appId + "&sip_apiname=" + apiname + "&sip_timestamp=" + timestamp + "&sip_sign=" + sipsign;
            //接口级参数拼装
            string apidate = "&domainid=" + "3" + "&userid=" + userId;

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] postdata = encoding.GetBytes(SIPdate + apidate);//所有要传参数拼装
            // Prepare web request...
            //目前阿里软件的服务集成平台（SIP）的接口
            //测试地址是：http://sipdev.alisoft.com/sip/rest，软件测试开发调用
            //正式环境地址是：http://sip.alisoft.com/sip/rest,软件发布上线后调用。
            //这里使用正式环境URL,测试环境使用http://sipdev.alisoft.com/sip/rest。

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://sipdev.alisoft.com/sip/rest");
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = postdata.Length;
            Stream newStream = myRequest.GetRequestStream();
            // Send the data.
            newStream.Write(postdata, 0, postdata.Length);
            newStream.Close();
            // Get response

            //以下内容为接口返回结果解析
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            string status = myResponse.Headers.Get("sip_status");//获得接口返回状态码

            if (status != null)
            {
                //Sip返回结果为成功的情况
                if ("0".Equals(status))
                {
                    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    string content = reader.ReadToEnd();
                    //解析接口返回值，这里选用XML格式的解析，接口默认返回是XML格式
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(content);
                    XmlNode xn = xmlDoc.SelectSingleNode("String");
                    XmlElement xe = (XmlElement)xn;
                    string result = xe.InnerText;
                    Label1.Text = (result);//用于页面显示
                }
                //SIP返回错误信息解析,此处为了说明各返回值，ISV可不用处理这么详细
                else
                {
                    Label1.Text = (status);//用于页面显示
                }
            }
        }
    }
}
